#!/usr/bin/python
# -*- coding: UTF-8 -*-
import yaml
import json

# yml多语言目录
ymlLangDir = '.'
dataJson = {}
dataMap = {}

def read_yaml(yaml_file):
    with open(yaml_file, 'r', encoding='utf-8') as file:
        data = yaml.safe_load(file)
    return data

def get_value(data, key):
    if data is None:
        return ''
    if key in data:
        return data[key]
    return ''

def get_dir_str_file_name(fileDir):
    return fileDir.split('/')[-1].split('.')[0]

def get_all_yml_files(yml_lang_dir):
    import os
    ymlFiles = []
    for root, dirs, files in os.walk(yml_lang_dir):
        for file in files:
            if file.endswith('.yml'):
                ymlFiles.append(os.path.join(root, file))
    return ymlFiles

def init_json():
  global dataMap
  fileList = get_all_yml_files(ymlLangDir)

  for fileDir in fileList:
    if dataMap.get(get_dir_str_file_name(fileDir)) is None:
      dataMap[get_dir_str_file_name(fileDir)] = read_yaml(fileDir)
    else:
      dataMap[get_dir_str_file_name(fileDir)].update(read_yaml(fileDir))

  for fileDir in fileList:
    if fileDir.find('zh-CN.yml') != -1:
      data1 = read_yaml(fileDir) or {}
      for key in data1:
        dataJson[key] = {
          'zh-CN': get_value(dataMap['zh-CN'], key),
          'en-US': get_value(dataMap['en-US'], key),
          'hi-IN': get_value(dataMap['hi-IN'], key),
          'pt-BR': get_value(dataMap['pt-BR'], key),
          'th-TH': get_value(dataMap['th-TH'], key),
          'vi-VN': get_value(dataMap['vi-VN'], key),
          'in-ID': get_value(dataMap['in-ID'], key),
          'ja-JP': get_value(dataMap['zh-CN'], key),
          'ko-KR': get_value(dataMap['zh-CN'], key),
        }

init_json()

with open('global.json', 'w', encoding='utf-8') as f:
    json.dump(dataJson, f, ensure_ascii=False, indent=2)
